//移动端尺寸
var screenwidth = window.screen.availWidth;//屏幕宽度
var documentwidth = document.documentElement.clientWidth;//页面DOM宽度
var containerwidth = documentwidth*0.92;//容器宽度
var cellwidth = documentwidth*0.18;//单元格宽度
var cellspacewidth = documentwidth*0.04;//单元格的间隔宽度

//获取距离上边的位置
function getPosTop(i,j){
	return cellspacewidth+(cellspacewidth+cellwidth)*i;
}

//获取距离左边的位置
function getPosLeft(i,j){
	return cellspacewidth+(cellspacewidth+cellwidth)*j;
}

//获取数字的背景颜色
function getNumberBackgroundColor(num){
	switch(num){
		case 2:return "rgba(255,0,0,0.8)";break;
		case 4:return "rgba(0,255,0,0.8)";break;
		case 8:return "rgba(0,0,255,0.8)";break;   
		case 16:return "rgba(0,255,255,0.8)";break;
		case 32:return "rgba(255,0,0,0.5)";break;
		case 64:return "rgba(0,255,0,0.5)";break;
		case 128:return "rgba(0,0,255,0.5)";break;
		case 256:return "rgba(0,255,255,0.5)";break;
		case 512:return "rgba(255,0,0,0.2)";break;
		case 1024:return "rgba(0,255,0,0.2)";break;
		case 2048:return "rgba(0,0,255,0.2)";break;
		default:break;
	}
}

function getNumberColor(num){
	if(num<=4){
		return "#000";
	}else{
		return "#fff";
	}
}

//是否没有空间
function noSpace(nums){
	for(var i=0;i<4;i++){
		for(var j=0;j<4;j++){
			if(nums[i][j] == 0){
				return false;
			}
		}
	}
	return true;
}

//判断是否可以向左移动
function canMoveLeft(nums){
	for(var i=0;i<4;i++){
		for(var j=1;j<4;j++){
			if(nums[i][j] != 0){
				if(nums[i][j-1] == 0 || nums[i][j-1] == nums[i][j]){
					return true;
				}
			}
		}
	}
	return false;
}

//判断是否可以向右移动
function canMoveRight(nums){
	for(var i=0;i<4;i++){
		for(var j=0;j<4;j++){
			if(nums[i][j] != 0){
				if(nums[i][j+1] == 0 || nums[i][j+1] == nums[i][j]){
					return true;
				}
			}
		}
	}
	return false;
}

//判断是否可以向上移动
function canMoveUp(nums){
	for(var i=1;i<4;i++){
		for(var j=0;j<4;j++){
			if(nums[i][j] != 0){
				if(nums[i-1][j] == 0 || nums[i-1][j] == nums[i][j]){
					return true;
				}
			}
		}
	}
	return false;
}

//判断是否可以向下移动
function canMoveDown(nums){
	for(var i=0;i<3;i++){
		for(var j=0;j<4;j++){
			if(nums[i][j] != 0){
				if(nums[i+1][j] == 0 || nums[i+1][j] == nums[i][j]){
					return true;
				}
			}
		}
	}
	return false;
}

//判断水平方向上是否没有障碍物
function noBlockHorizontal(row,col1,col2,num){
	for(var i=col1+1;i<col2;i++){
		if(num[row][i]!=0){
			return false;
		}
	}
	return true;
}

//判断垂直方向上是否没有障碍物
function noBlockVertical(col,row1,row2,num){
	for(var i=row1+1;i<row2;i++){
		if(num[i][col]!=0){
			return false;
		}
	}
	return true;
}
//更新分数
function updateScore(score){
	$('#score').text(score)
}

function noMove(nums){
	if(canMoveLeft(nums) || canMoveRight(nums) || canMoveUp(nums) || canMoveDown(nums)){
		return false;
	}
	return true;
}
//判断游戏是否结束 1.没有空的单元格2.不能移动
function isGameover(){
	if(noSpace(nums) && noMove(nums)){
		var r = window.confirm('Game over!Newgame?');
		if(r){
			newgame()
		}else{
			alert("所得分数为"+score)
		}
	}
}